package cn.topevery.garden.wapi;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import hw.topevery.framework.serializer.SqlDateSerializer;
import hw.topevery.framework.serializer.SqlTimeSerializer;
import hw.topevery.framework.serializer.SqlTimestampSerializer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

@ComponentScan({
        "hw.topevery.framework.si",
        "hw.topevery.cloud.client",
        "hw.topevery.fmp.**",
        "hw.topevery.af.**",
        "hw.topevery.basis.*",
        "hw.topevery.sso.**",
        "cn.topevery.*"
})
@EnableScheduling //换成xxl-job 这个不要开启-- by wilson.fu 2022-8-25
@EnableCaching
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class, SecurityAutoConfiguration.class, OAuth2ResourceServerAutoConfiguration.class})
public class GardenWapiApplication  implements CommandLineRunner {

/*    @NacosInjected
    NamingService namingService;

    @NacosValue(value = "${spring.application.name}", autoRefreshed = true)
    String applicationName;

    @NacosValue(value = "${server.port:8103}", autoRefreshed = true)
    Integer serverPort;


    @NacosValue(value = "${nacos.discovery.group}", autoRefreshed = true)
    String groupName;

    @NacosValue(value = "${nacos.discovery.register.ip}", autoRefreshed = true)
    String registerIp;*/


/*    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        fastConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastConverter;
        return new HttpMessageConverters(converter);
    }*/

    @Bean(name = "remoteRestTemplate")
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(GardenWapiApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
/*        String localip = "";
        if(StringUtils.isEmpty(registerIp)) {
            localip = IpUtils.getLocalIp();
        }
        else{
            localip = registerIp;
        }

        System.out.println(String.format("Nacos registerInstance applicationName:%s,groupName:%s,registerIp:%s,serverPort:%d"
                , applicationName, groupName, localip, serverPort));
        namingService.registerInstance(this.applicationName, this.groupName, localip, this.serverPort);*/

    }
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        //1.需要定义一个Convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        //2.添加fastjson的配置信息，比如是否要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        SerializerFeature[] serializerFeatures = {
                SerializerFeature.PrettyFormat,
                // 是否输出为null的字段,若为null 则显示该字段
                SerializerFeature.WriteMapNullValue,
                // 禁止循环引用
                SerializerFeature.DisableCircularReferenceDetect
        };
        fastJsonConfig.setSerializerFeatures(serializerFeatures);
        //3.在convert中添加配置信息

        fastJsonConfig.getSerializeConfig().put(java.sql.Date.class, new SqlDateSerializer());
        fastJsonConfig.getParserConfig().putDeserializer(java.sql.Date.class, new SqlDateSerializer());

        fastJsonConfig.getSerializeConfig().put(java.sql.Timestamp.class, new SqlTimestampSerializer());
        fastJsonConfig.getParserConfig().putDeserializer(java.sql.Timestamp.class, new SqlTimestampSerializer());

        fastJsonConfig.getSerializeConfig().put(java.sql.Time.class, new SqlTimeSerializer());
        fastJsonConfig.getParserConfig().putDeserializer(java.sql.Time.class, new SqlTimeSerializer());

        fastConverter.setFastJsonConfig(fastJsonConfig);
        return new HttpMessageConverters(fastConverter);
    }
}


